package com.bsearchives;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bsearchives.entity.Script;

@SuppressWarnings("serial")
public class BsearchivesServlet extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			resp.setContentType("text/html");
//			PrintWriter out = resp.getWriter();

			URL url = new URL(
					"http://www.bseindia.com/bhavcopy/eq050609_csv.zip");
			ZipInputStream in = new ZipInputStream(new BufferedInputStream(url
					.openStream()));
			ZipEntry file;
			List<Script> scripts = new ArrayList<Script>();
			while ((file = in.getNextEntry()) != null) {
//				out.println(file.getName());
				InputStreamReader isr = new InputStreamReader(in);
				BufferedReader reader = new BufferedReader(isr);
				String line;
				while ((line = reader.readLine()) != null) {
					//out.println("<li>"+ line+"</li>");
					Script script = createScriptObject(line);
					scripts.add(script);
					//System.out.println(script);
				}
				
				req.getSession().setAttribute("SCRIPTS", scripts);
//				resp.sendRedirect("archives.jsp");
				RequestDispatcher rd = req.getRequestDispatcher("archives.jsp");
				rd.forward(req, resp);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		finally{
			
		}

	}
	private Script createScriptObject(String line) {
		StringTokenizer st = new StringTokenizer(line, ",");
		Script script = new Script();
		script.setDate(new Date());
		String token = st.nextToken();
		if ("SC_CODE".equalsIgnoreCase(token)) {
			return null; // Header Row
		} else {
			// SC_CODE
			script.setScCode(token);

			// SC_NAME
			if (st.hasMoreTokens()) {
				token = st.nextToken();
				if (token != null)
					script.setScName(token);
			}

			// SC_GROUP
			if (st.hasMoreTokens()) {
				token = st.nextToken();
				if (token != null)
					script.setScGrp(token);
			}

			// SC_TYPE
			if (st.hasMoreTokens()) {
				token = st.nextToken();
				if (token != null)
					script.setScType(token);
			}

			// OPEN
			if (st.hasMoreTokens()) {
				token = st.nextToken();
				if (token != null)
					script.setOpenPrice(token);
			}

			// HIGH
			if (st.hasMoreTokens()) {
				token = st.nextToken();
				if (token != null)
					script.setHighPrice(token);
			}

			// LOW
			if (st.hasMoreTokens()) {
				token = st.nextToken();
				if (token != null)
					script.setLowPrice(token);
			}
			// CLOSE
			if (st.hasMoreTokens()) {
				token = st.nextToken();
				if (token != null)
					script.setClosePrice(token);
			}
			// LAST
			if (st.hasMoreTokens()) {
				token = st.nextToken();
				if (token != null)
					script.setLastTradedPrice(token);
			}
			// PREVCLOSE
			if (st.hasMoreTokens()) {
				token = st.nextToken();
				if (token != null)
					script.setPrevClose(token);
			}

			// NO_TRADES
			if (st.hasMoreTokens()) {
				token = st.nextToken();
				if (token != null)
					script.setNoOfShares(token);
			}

			// NO_OF_SHRS
			if (st.hasMoreTokens()) {
				token = st.nextToken();
				if (token != null)
					script.setNoOfShares(token);
			}

			// NET_TURNOV
			if (st.hasMoreTokens()) {
				token = st.nextToken();
				if (token != null)
					script.setNetTurnOver(token);
			}

			// TDCLOINDI
			if (st.hasMoreTokens()) {
				token = st.nextToken();
				if (token != null)
					script.setTdcloindi(token);
			}
		}
		return script;
	}
}
